package com.carhailing.alipay.controller;

import com.carhailing.alipay.config.AliPayResource;
import com.carhailing.alipay.service.IMyAlipayService;
import com.carhailing.common.domain.OrderTrade;
import com.carhailing.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


/**
 * 支付层
 */
@Slf4j
@RestController
@RequestMapping("payment")
public class MyAlipayController {

   @Autowired
   private AliPayResource aliPayResource;

   @Autowired
   private IMyAlipayService iMyAlipayService;

   /**
    * 获取下单的token；防止重复下单
    *
    * @return
    */
   @GetMapping("getOrderToken")
   public Result<String> getOrderToken() {
      String orderToken = iMyAlipayService.getOrderToken();
      return Result.success(orderToken);
   }

   /**
    * @param orderTrade 订单信息
    * @param orderToken 下单的token
    * @return
    */
   @GetMapping("underOrder")
   public Result<String> goUnderOrder(OrderTrade orderTrade, String orderToken) {
      iMyAlipayService.goUnderOrder(orderTrade, orderToken);
      return Result.success();
   }

   /**
    * 支付宝支付
    *
    * @param orderNum 订单号
    * @param pattern  支付方式
    * @return
    */
   @GetMapping("/goPay/{orderNum}/{pattern}")
   public Result<String> goPay(@PathVariable("orderNum") String orderNum, @PathVariable("pattern") String pattern) {
      String string = iMyAlipayService.goPay(orderNum, pattern);
      return Result.success(string);
   }


   /**
    * 支付成功后的支付宝异步通知,主动发给我信息交易结果
    */
   @RequestMapping(value = "/alipay")
   public void paymentCallback(HttpServletRequest request) {
      String s = iMyAlipayService.paymentCallback(request);
   }

   /**
    * 主动调查询支付结果
    */
   @GetMapping(value = "/queryAlipayResult")
   public void queryAlipayResult(String num, String alipayNum) {
      String s = iMyAlipayService.queryAlipayResult(num, alipayNum);

   }
}